﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Abs.App.Booking
{
    public partial class Check_Availability : System.Web.UI.Page
    {
        private string adminMaster = "~/MasterPages/AdminMaster.Master";
        private string userMaster = "~/MasterPages/UserMaster.Master";
        
        protected void Page_PreInit(object sender, EventArgs e)
        {
            //Determine which master page to use based on the role of the account (normal user/ admin)
            if (Session["Role"] != null && ((int)Session["Role"] == (int)Abs.Core.Models.User.RoleEnum.Admin))
            {
                MasterPageFile = adminMaster;
            }
            else
            {
                MasterPageFile = userMaster;
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            if (datepicker.Text == "____/__/__")
            {
                return;
            }

            System.Threading.Thread.Sleep(3000); //To display the update progress AJAX
            Table_Display.Visible = true;
            Table_Legend.Visible = true;
            var roomManager = Abs.Core.Management.Manage.Room;
            int dayId = (int)DateTime.Parse( datepicker.Text).DayOfWeek;
            var roomList = roomManager.GetRoomsOnDay(dayId);
            var appManager = Abs.Core.Management.Manage.Appointment;
            var availableRoom = appManager.GetAvailableRoomsOnDate(datepicker.Text);
            foreach (var room in availableRoom)
            {
                TableRow row = new TableRow();
                for (int i = 0; i < 2; i++)
                {
                    
                    //
                    if (i == 0)
                    {
                        TableCell cell = new TableCell();
                        Label label = new Label();
                        label.Text = room.Value.RoomName;
                        cell.Controls.Add(label);
                        row.Controls.Add(cell);
                    }
                    else if (i == 1)
                    {
                        for (int hour = (int)Abs.Core.Models.Room.Schedule.Start; hour < (int)Abs.Core.Models.Room.Schedule.End; hour++)
                        {
                            TableCell cell = new TableCell();
                            if (room.Value.IsAvailableAtTime[hour])
                            {
                                cell.CssClass = "Available";
                            }
                            else
                            {
                                cell.CssClass = "Busy";
                            }
                            
                            row.Controls.Add(cell);
                        }
                    }                  
                }
                Table_Display.Controls.Add(row);
            }
        }

    }
}